Utilisateur:Lgd/sondache/sondache.js
< Utilisateur:Lgd | sondache
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5./**
* Calcul de résultats de votes.
* auteur : http://fr.wikipedia.org/wiki/Utilisateur:Lgd
* licence : MIT
*
* Aide et paramétrage : http://fr.wikipedia.org/wiki/Utilisateur:Lgd/sondache
*/
/* globals mw, $ */
$.when(mw.loader.using('mediawiki.util'), $.ready).done(function () {
function result(labeloui, labelnon) {
for (var i = 2; i < 6; i++) {
var Hn = 'h' + i;
var $pour = $(Hn + ':contains(' + labeloui + ')');
$pour.each(function () {
var $this = $(this);
if ($this.find('.result').length !== 0) {
return;
}
var oui = $this.nextUntil(Hn, 'ol').find('li').not('.mw-empty-elt').length;
var $hNnon = $this.nextUntil(Hn + ':contains(' + labeloui + ')', Hn + ':contains(' + labelnon + '):first');
var non = $hNnon.next('ol').find('li').not('.mw-empty-elt').length;
var $hNneutre = $this.nextUntil(Hn + ':contains(' + labeloui + ')', Hn + ':contains("Neutre"):first');
var neutre = $hNneutre.next('ol').find('li').not('.mw-empty-elt').length;
var result1 = ((oui / (oui + non)) * 100).toFixed(1);
var result2 = ((oui / (oui + non + neutre)) * 100).toFixed(1);
$this.append('<span class="result"> <span style="background: red; display: inline-block; width: 100px; border: 1px solid #aaa"><span style="background: lightgreen; display: inline-block; width: '+result1+'px"> </span></span> ' + result1 + ' % des (' + labeloui + '+' + labelnon + ') — ' + result2 + ' % des (' + labeloui + '+' + labelnon + '+neutre)</span>');
result1 = ((non / (oui + non)) * 100).toFixed(1);
result2 = ((non / (oui + non + neutre)) * 100).toFixed(1);
$hNnon.append('<span class="result"> <span style="background: lightgreen; display: inline-block; width: 100px; border: 1px solid #aaa"><span style="background: red; display: inline-block; width: '+result1+'px"> </span></span> ' + result1 + ' % des (' + labeloui + '+' + labelnon + ') — ' + result2 + ' % des (' + labeloui + '+' + labelnon + '+neutre)</span>');
result2 = ((neutre / (oui + non + neutre)) * 100).toFixed(1);
$hNneutre.append('<span class="result"> <span style="background: #fff; display: inline-block; width: 100px; border: 1px solid #aaa"><span style="background: lightgray; display: inline-block; width: '+result2+'px"> </span></span> ' + result2 + ' % des (' + labeloui + '+' + labelnon + '+neutre)</span>');
resultOk = true;
});
}
}
var resultOk = false;
var resultFunctionLink = mw.util.addPortletLink('p-cactions', '#', 'Calculer les résultats', 'ca-result');
$(resultFunctionLink).click(function () {
$(':header .result').remove();
result('Pour', 'Contre');
result('Oui', 'Non');
result('Approbation', 'Opposition');
result('Conserver', 'Supprimer');
if (resultOk) {
$('#ca-result').find('a').css('color', '#aaa');
}
});
});